package com.koron.sinoprobe.web.subpackage.service.impl;

import cn.hutool.core.util.StrUtil;
import com.koron.common.core.util.CodeTools;
import com.koron.sinoprobe.web.subpackage.bean.SubPackageDetail;
import com.koron.sinoprobe.web.subpackage.bean.dto.SubPackageDetailDTO;
import com.koron.sinoprobe.web.subpackage.mapper.SubPackageDetailMapper;
import com.koron.sinoprobe.web.subpackage.service.SubPackageDetailService;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@Service
public class SubPackageDetailServiceImpl implements SubPackageDetailService {
    @TaskAnnotation("addSubPackageDetail")
    @Override
    public SubPackageDetailDTO addSubPackageDetail(SessionFactory factory, SubPackageDetailDTO subPackageDetailDTO) {
        Assert.notNull(subPackageDetailDTO, "施工分包对象不能为空！");
        SubPackageDetailMapper mapper = factory.getMapper(SubPackageDetailMapper.class);
        SubPackageDetail subPackageDetail = convertToSubPackageDetail(subPackageDetailDTO);
        mapper.saveSubPackageDetail(subPackageDetail);
        return subPackageDetailDTO;
    }

    @TaskAnnotation("batchSave")
    @Override
    public List<SubPackageDetailDTO> batchSave(SessionFactory factory, List<SubPackageDetailDTO> list) {
        if (CollectionUtils.isEmpty(list)){
            throw new RuntimeException("list is empty!");
        }
        SubPackageDetailMapper mapper = factory.getMapper(SubPackageDetailMapper.class);
        list.stream().forEach(subPackageDetailDTO-> {
            SubPackageDetail subPackageDetail = convertToSubPackageDetail(subPackageDetailDTO);
            mapper.saveSubPackageDetail(subPackageDetail);
        });
        return list;
    }

    @TaskAnnotation("getSubPackageDetailByPackageId")
    @Override
    public List<SubPackageDetailDTO> getSubPackageDetailByPackageId(SessionFactory factory, String packageId) {
        Assert.notNull(packageId, "packageId不能为空！");
        SubPackageDetailMapper mapper = factory.getMapper(SubPackageDetailMapper.class);
        List<SubPackageDetail> subPackageDetails = mapper.getSubPackageDetailByPackageId(packageId);
        List<SubPackageDetailDTO> result = subPackageDetails.stream().map(s -> convertToSubPackageDetailDto(s)).collect(Collectors.toList());
        return result;
    }

    @TaskAnnotation("getSubPackageDetailById")
    @Override
    public SubPackageDetailDTO getSubPackageDetailById(SessionFactory factory, String id) {
        Assert.notNull(id, "packageId不能为空！");
        SubPackageDetailMapper mapper = factory.getMapper(SubPackageDetailMapper.class);
        SubPackageDetail subPackageDetail = mapper.getSubPackageDetailById(id);
        SubPackageDetailDTO subPackageDetailDTO = convertToSubPackageDetailDto(subPackageDetail);
        return subPackageDetailDTO;
    }

    @TaskAnnotation("updateSubPackageDetail")
    @Override
    public SubPackageDetailDTO updateSubPackageDetail(SessionFactory factory, SubPackageDetailDTO subPackageDetailDTO) {
        Assert.notNull(subPackageDetailDTO, "施工分包对象不能为空！");
        Assert.notNull(subPackageDetailDTO.getId(), "施工分包对象id不能为空！");
        SubPackageDetailMapper mapper = factory.getMapper(SubPackageDetailMapper.class);
        SubPackageDetail subPackageDetail = convertToSubPackageDetail(subPackageDetailDTO);
        mapper.updateSubPackageDetail(subPackageDetail);
        return subPackageDetailDTO;
    }

    @TaskAnnotation("deleteSubPackageDetail")
    @Override
    public int deleteSubPackageDetail(SessionFactory factory, String id) {
        Assert.notNull(id, "id不能为空！");
        SubPackageDetailMapper mapper = factory.getMapper(SubPackageDetailMapper.class);
        return  mapper.deleteSubPackageDetail(id);
    }

    private SubPackageDetail convertToSubPackageDetail(SubPackageDetailDTO subPackageDetailDTO) {
        // 手动进行属性转换，将 String 类型转换为 BigDecimal 类型

        if (StrUtil.isEmpty(subPackageDetailDTO.getId())){
            subPackageDetailDTO.setId(CodeTools.getCode32());
            subPackageDetailDTO.setCode(CodeTools.getCodeNo(SubPackageDetail.PREFIX, 3));
        }

        SubPackageDetail subPackageDetail = new SubPackageDetail();
        subPackageDetail.setId(subPackageDetailDTO.getId());
        subPackageDetail.setCode(subPackageDetailDTO.getCode());
        subPackageDetail.setPackageId(subPackageDetailDTO.getPackageId());
        subPackageDetail.setTitle(subPackageDetailDTO.getTitle());
        subPackageDetail.setUnit(subPackageDetailDTO.getUnit());
        subPackageDetail.setContractQuantity(subPackageDetailDTO.getContractQuantity());
        subPackageDetail.setUnitPrice(new BigDecimal(subPackageDetailDTO.getUnitPrice()));
        subPackageDetail.setContractAmount(new BigDecimal(subPackageDetailDTO.getContractAmount()));
        subPackageDetail.setSubcontractAmount(new BigDecimal(subPackageDetailDTO.getSubcontractAmount()));
        subPackageDetail.setSubcontractQuantity(subPackageDetailDTO.getSubcontractQuantity());
        subPackageDetail.setSubcontractPercentage(subPackageDetailDTO.getSubcontractPercentage());
        subPackageDetail.setCreateBy(subPackageDetailDTO.getCreateBy());
        subPackageDetail.setCreateByName(subPackageDetailDTO.getCreateByName());
        subPackageDetail.setCreateTime(subPackageDetailDTO.getCreateTime());
        subPackageDetail.setUpdateBy(subPackageDetailDTO.getUpdateBy());
        subPackageDetail.setUpdateByName(subPackageDetailDTO.getUpdateByName());
        subPackageDetail.setUpdateTime(subPackageDetailDTO.getUpdateTime());
        return subPackageDetail;
    }


    private SubPackageDetailDTO convertToSubPackageDetailDto(SubPackageDetail subPackageDetail) {
        // 手动进行属性转换，将 BigDecimal 类型转换为 String 类型

        SubPackageDetailDTO subPackageDetailDTO = new SubPackageDetailDTO();
        subPackageDetailDTO.setId(subPackageDetail.getId());
        subPackageDetailDTO.setCode(subPackageDetail.getCode());
        subPackageDetailDTO.setPackageId(subPackageDetail.getPackageId());
        subPackageDetailDTO.setTitle(subPackageDetail.getTitle());
        subPackageDetailDTO.setUnit(subPackageDetail.getUnit());
        subPackageDetailDTO.setContractQuantity(subPackageDetail.getContractQuantity());
        subPackageDetailDTO.setUnitPrice(subPackageDetail.getUnitPrice().toString());
        subPackageDetailDTO.setContractAmount(subPackageDetail.getContractAmount().toString());
        subPackageDetailDTO.setSubcontractAmount(subPackageDetail.getSubcontractAmount().toString());
        subPackageDetailDTO.setSubcontractQuantity(subPackageDetail.getSubcontractQuantity());
        subPackageDetailDTO.setSubcontractPercentage(subPackageDetail.getSubcontractPercentage());
        subPackageDetailDTO.setCreateBy(subPackageDetail.getCreateBy());
        subPackageDetailDTO.setCreateByName(subPackageDetail.getCreateByName());
        subPackageDetailDTO.setCreateTime(subPackageDetail.getCreateTime());
        subPackageDetailDTO.setUpdateBy(subPackageDetail.getUpdateBy());
        subPackageDetailDTO.setUpdateByName(subPackageDetail.getUpdateByName());
        subPackageDetailDTO.setUpdateTime(subPackageDetail.getUpdateTime());
        return subPackageDetailDTO;
    }
}
